/**
 * AddCircleMemberActivity.java
 * com.jamin.aabill.ui
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-30 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.jamin.aabill.R;
import com.jamin.aabill.custom.TopBar;
import com.jamin.aabill.custom.TopBarClickListener;
import com.jamin.aabill.dao.CircleDao;
import com.jamin.aabill.dao.CircleMemberDao;
import com.jamin.aabill.data.Circle;
import com.jamin.aabill.data.CircleMember;


/**
 * ClassName:AddCircleMemberActivity
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-30 上午8:47:55 
 */
public class AddCircleMemberActivity extends Activity implements OnClickListener{

	private TopBar topBar = null;
	private EditText mNameEditText = null;
	private EditText mMoblileEditText = null;
	private EditText mDescriptionEditText = null;
	private Spinner mCircleSpinner = null;
	private Button mCommitBtn = null; 
	private Button mGoAddMemberBtn = null;
	private long circleId = -1;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_circle_member_activity);
		circleId = getIntent().getLongExtra("circleId", circleId);
		initView();
		initData();
	}



	private void Commit() {
		if(!TextUtils.isEmpty(mNameEditText.getText().toString())){
			CircleMemberDao mDao = new CircleMemberDao();
			CircleMember mCircleMember = new CircleMember(
					mNameEditText.getText().toString(), 
					circleId,
					mMoblileEditText.getText().toString(),
					mDescriptionEditText.getText().toString());
			mDao.replace(mCircleMember);
			Toast.makeText(this, getString(R.string.add_circle_member_activity_create_ok), Toast.LENGTH_SHORT).show();
		}else{
			Animation shake = AnimationUtils.loadAnimation(AddCircleMemberActivity.this, R.anim.shake);
			mNameEditText.setAnimation(shake);
		}

	}


	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.add_circle_member_ok:
			Commit();
			finish();
			break;
		case R.id.add_circle_member_go_on:
			Commit();
			mNameEditText.setText("");
			mMoblileEditText.setText("");
			mDescriptionEditText.setText("");
			break;
		}

	}

	private void initView() {
		topBar = (TopBar) findViewById(R.id.top_bar);
		mNameEditText = (EditText) findViewById(R.id.add_circle_member_name);
		mMoblileEditText = (EditText) findViewById(R.id.add_circle_member_mobile);
		mDescriptionEditText = (EditText) findViewById(R.id.add_circle_member_description);
		mCircleSpinner = (Spinner) findViewById(R.id.add_circle_member_spinner);
		mCommitBtn = (Button) findViewById(R.id.add_circle_member_ok);
		mGoAddMemberBtn = (Button) findViewById(R.id.add_circle_member_go_on);
		mGoAddMemberBtn.setOnClickListener(this);
		mCommitBtn.setOnClickListener(this);
		topBar.setTopBarClickListener(new TopBarClickListener() {
			@Override
			public void rightBtnClick() {
			}
			@Override
			public void right2BtnClick() {
			}
			@Override
			public void leftBtnClick() {
				finish();
			}
		});


	}




	private void initData() {
		CircleDao mCircleDao = new CircleDao();
		Circle circle = mCircleDao.getCircle(circleId);
		List<Circle> list = new ArrayList<Circle>();
		list.add(circle);
		String [] strArray  = new String[list.size()];
		for(int i = 0; i< list.size(); i++){
			strArray[i] = list.get(i).getCircle_name();
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item , strArray);
		//		adapter.setDropDownViewResource(R.layout.);
		mCircleSpinner.setAdapter(adapter);
	}

}

